package com.example.progressrecord

import android.app.DatePickerDialog
import android.app.Dialog
import android.app.TimePickerDialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.DatePicker
import android.widget.TimePicker
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.lifecycle.lifecycleScope
import com.example.progressrecord.databinding.DialogAddProgressBinding
import com.example.progressrecord.leancloud.ProgressInfo
import com.example.progressrecord.leancloud.ProgressInfoRepository.save
import com.example.progressrecord.utils.DateTimeUtils
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import java.util.*

class AddProgressInfoFragment : BottomSheetDialogFragment() {

    private lateinit var binding: DialogAddProgressBinding

    companion object {
        const val ADDED_INFO = "ADDED_INFO"
    }


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return AddProgressInfoDialog()
    }

    inner class AddProgressInfoDialog : BottomSheetDialog(context!!) {
        init {
            binding = DialogAddProgressBinding.inflate(layoutInflater)
            setContentView(binding.root)
            binding.timeInput.text = DateTimeUtils.instance().getTimeStr()
            binding.dateInput.setOnClickListener {
                showDateSelectDialog()
            }
            binding.timeInput.setOnClickListener {
                showTimePickerDialog()
            }
            binding.confirmTv.setOnClickListener {
                saveInfo()
            }
        }
    }

    var onDismiss: (() -> Unit)? = null
    private val savedInfo by lazy { ProgressInfo() }
    private val savedTime by lazy { Calendar.getInstance() }
    private fun saveInfo() {
        savedInfo.creatinine = binding.creatinineInput.text.toString()
            .ifEmpty {
                toastTips(getTips(R.string.creatinine))
                return@saveInfo
            }.toFloat()
        savedInfo.urine24hVolume = binding.volumeInput.text.toString()
            .ifEmpty {
                toastTips(getTips(R.string.urine24h_volume))
                return@saveInfo
            }.toFloat()
        savedInfo.urineProtein24hConcentration =
            binding.concentrationInput.text.toString()
                .ifEmpty {
                    toastTips(getTips(R.string.urine24h_concentration))
                    return@saveInfo
                }.toFloat()
        savedInfo.remark = binding.remarkInput.text.toString()
        savedInfo.time = savedTime.time
        lifecycleScope.launch {
            savedInfo.save()?.let {
                arguments = Bundle().apply { putParcelable(ADDED_INFO, savedInfo) }
                onDismiss?.invoke()
                dismiss()
            }
        }

    }

    private fun getTips(content: Int) = getString(R.string.empty_tips, getString(content))

    private fun toastTips(tips: String) {
        Toast.makeText(requireContext(), tips, Toast.LENGTH_LONG).show()
    }

    private val timePickerDialog by lazy {
        context?.let {
            TimePickerDialog(
                it, R.style.MyDialog,
                { p0, p1, p2 ->
                    binding.timeInput.text = "$p1:$p2".apply {
                        savedTime.set(Calendar.HOUR_OF_DAY, p1)
                        savedTime.set(Calendar.MINUTE, p2)
                    }
                },
                DateTimeUtils.instance().getHourNow(),
                DateTimeUtils.instance().getMinuteNow(),
                true
            )
        }
    }

    private fun showTimePickerDialog() {
        timePickerDialog?.show()
    }

    private val datePickerDialog by lazy {
        context?.let {
            DatePickerDialog(
                it, R.style.MyDialog,
                { p0, p1, p2, p3 ->
                    binding.dateInput.text =
                        listOf(p1, p2, p3).forDateStr().apply {
                            savedTime.set(Calendar.YEAR, p1)
                            savedTime.set(Calendar.MONTH, p2)
                            savedTime.set(Calendar.DAY_OF_MONTH, p3)
                        }
                },
                DateTimeUtils.instance().getYearNow(),
                DateTimeUtils.instance().getMonthNow(),
                DateTimeUtils.instance().getDayNow()
            )
        }
    }

    private fun showDateSelectDialog() {
        datePickerDialog?.show()
    }
}